﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ClassLibrary.BLL;

namespace Web
{
    public partial class ChiTiet : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BLLSanPham sp = new BLLSanPham(Request.QueryString["ID"]);
                DataList1.DataSource = sp.GetByID();
                DataList1.DataBind();
            }
        }

        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "datmua")
            {
                GioHang gio = (GioHang)Session["GioHang"];
                string id = e.CommandArgument.ToString();
                Label thongbao = (Label)e.Item.FindControl("Lbthongbao");
                Label ten = (Label)e.Item.FindControl("LbTenSP");
                Label gia = (Label)e.Item.FindControl("LbGia");
                TextBox sl = (TextBox)e.Item.FindControl("TxtSoLuong");

                if (!gio.Exits(id))
                {
                    gio.Add(id, ten.Text, int.Parse(sl.Text), long.Parse(gia.Text));
                    Session["sohang"] = (int)Session["sohang"] + 1;
                   
                   Response.Redirect("ChiTiet.aspx?ID=" + id + "");
                }
                else
                {
                    if (gio.CheckSoluong(id) > 9)
                    {
                        thongbao.Text = "Số lượng mua vượt quá 10 ";
                    }
                    else
                    {
                        gio.Add(id, ten.Text, int.Parse(sl.Text), long.Parse(gia.Text));
                        Response.Redirect("ChiTiet.aspx?ID=" + id + "");
                        // thongbao.Text = gio.CheckSoluong(id).ToString();
                    }
                }
            }
        }
    }
}
